﻿/*
 *Author: Jeff Liu 
 *
 *Under MIT License
 *jebberwocky@gmail.com
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CtpGglMap.Core.StaticMap;

namespace CtpGglMap.Impl
{
    public class GPath : IMapItem
    {
        public String Color { get; set; }

        public String Weight { get; set; }

        public String FillColor { get; set; }

        private List<GeoPoint> points = new List<GeoPoint>();

        public GPath(List<GeoPoint> points)
        {
            this.points = points;
        }

        public void SetMap(IMap map)
        {
            if (map != null)
            {
                map.AddItem(this);
            }
        }

        public void AddPoint(GeoPoint point)
        {
            if (point != null)
            {
                if (!points.Exists(x => point.Equals(x)))
                {
                    points.Add(point);
                }
            }
        }

        public void RemovePoint(GeoPoint point)
        {
            if (point != null)
            {
                GeoPoint gp = points.SingleOrDefault(x => point.Equals(x));
                if (gp != null)
                    points.Remove(gp);
            }
        }

        public String GetUrl()
        {
            StringBuilder sb = new StringBuilder();

            List<String> param = new List<string>();

            if (!string.IsNullOrEmpty(Color))
                param.Add("color:" + Color);

            if (!string.IsNullOrEmpty(FillColor))
                param.Add("fillcolor:" + Color);

            if (!string.IsNullOrEmpty(Weight))
                param.Add("weight:" + Weight);

            foreach (GeoPoint gpoint in points)
            {
                param.Add(gpoint.Lat + "," + gpoint.Lng);
            }

            for (int i = 0; i < param.Count; i++)
            {
                sb.Append(param[i]);
                if (i < param.Count - 1)
                    sb.Append(Const.PIPE_SEPARATOR);
            }

            return sb.ToString();
        }
    }
}
